<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use swoole_channel;
use swoole_client;
use swoole_http_client;
use Symfony\Component\HttpFoundation\Response;
use swoole_serialize;

class DefaultController extends Controller
{
    /**
     * @Route("/test_server")
     */
    public function testServer(Request $request){

        return new Response("This is test server for swoole client!");
    }

    /**
     * @Route("/test_client")
     */
    public function indexAction(Request $request)
    {
        $client = new swoole_client(SWOOLE_SOCK_TCP);

        if (!$client->connect('127.0.0.1', 8007, -1))
        {
            die("connect failed.");
        }

        $data = serialize(['url'=>'/test_server','params'=>['p5','p6']]);
        if (!$client->send($data))
        {
            die("send failed.");
        }

        $data = $client->recv();
        if (!$data)
        {
            die("recv failed.");
        }
        echo $data;

        $client->close();
        exit;
    }
}
